package com.china08.yxyapi.api.manage.tec.teaching.weikecheng.weike;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.util.EsResourceUpdateUtil;

/**
 * 老师-----我的微课----共享到本校/教育云
 * 
 */
@CrossOrigin
@RestController
public class WeikechengShareController extends YxyApiBaseController {

    @Autowired
    private YxyResResourceRepository resourceRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/weikecheng/weike/share", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel,
            RespModel respModel) {
        reqModel.check();
        YxyResResource resource = resourceRepository.findById(reqModel.weikeId);
        resource.setViewScope(reqModel.viewScope);
        if (reqModel.viewScope == 2) {
            resource.setIsYxView(1);
        }
        resource.setStatus(0);
        resourceRepository.save(resource);

        // 重建索引
        EsResourceUpdateUtil.updateEsResoure(reqModel.weikeId);
        respModel = new RespModel();
        respModel.setMessage("共享到本校/教育云成功");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

        public String weikeId;// 微课id

        public Integer viewScope;// 共享范围 1 本校,2教育云

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(weikeId, null), "微课id不能为空");
            Assert.notNull(viewScope, "请选择共享范围");
        }

    }

    protected static class RespModel extends YxyApiRespModel {

    }
}